package my

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"gorm.io/gorm"
)

// Vulnerabilities表

type Vulnerability struct {
	gorm.Model
	Id            int     `gorm:"primary_key" json:"id"`
	NameZh        string  `gorm:"column:name_zh" json:"name_zh" binding:"required"`
	Cve           string  `gorm:"column:cve" json:"cve"`
	Cnnvd         string  `gorm:"column:cnnvd" json:"cnnvd"`
	Severity      string  `gorm:"column:severity" json:"severity"`
	Category      string  `gorm:"column:category" json:"category"`
	Description   string  `gorm:"type:longtext" json:"description"`
	Suggestion    string  `gorm:"type:longtext" json:"suggestion"`
	Language      string  `gorm:"column:language" json:"language"`
	Webapp        int     `gorm:"column:webapp" json:"webapp"`
	ForeignWebapp *Webapp `gorm:"foreignKey:Webapp"`
}

func (Vulnerability) TableName() string {
	return "my_vulnerabilities"
}

type VulnerabilitySearchField struct {
	Search        string
	CategoryField string
	WebappField   int
}

func GetVulnerabilitiesTotal(field *VulnerabilitySearchField) (total int64) {
	db := global.GVA_DB.Model(&Vulnerability{})

	if field.CategoryField != "" {
		db = db.Where("category = ?", field.CategoryField)
	}
	if field.WebappField != -1 {
		db = db.Where("webapp = ?", field.WebappField)
	}
	if field.Search != "" {
		db = db.Where(
			global.GVA_DB.Where("name_zh like ?", "%"+field.Search+"%").
				Or("cve like ?", "%"+field.Search+"%").
				Or("cnnvd like ?", "%"+field.Search+"%").
				Or("description like ?", "%"+field.Search+"%"))
	}
	db.Count(&total)
	return
}

func GetVulnerabilities(page int, pageSize int, field *VulnerabilitySearchField) (vuls []Vulnerability) {

	db := global.GVA_DB.Preload("ForeignWebapp")

	if field.CategoryField != "" {
		db = db.Where("category = ?", field.CategoryField)
	}
	if field.WebappField != -1 {
		db = db.Where("webapp = ?", field.WebappField)
	}
	if field.Search != "" {
		db = db.Where(
			global.GVA_DB.Where("name_zh like ?", "%"+field.Search+"%").
				Or("cve like ?", "%"+field.Search+"%").
				Or("cnnvd like ?", "%"+field.Search+"%").
				Or("description like ?", "%"+field.Search+"%"))
	}
	//	分页
	if page > 0 && pageSize > 0 {
		db = db.Offset((page - 1) * pageSize).Order("updated_at desc").Limit(pageSize).Find(&vuls)
	}
	return
}

func GetVulnerability(id int) (vul Vulnerability) {
	global.GVA_DB.Model(&Vulnerability{}).Where("id = ?", id).First(&vul)
	return
}

func EditVulnerability(id int, vul Vulnerability) bool {
	global.GVA_DB.Model(&Vulnerability{}).Where("id = ?", id).Updates(vul)
	return true
}

func AddVulnerability(vul Vulnerability) bool {
	global.GVA_DB.Create(&vul)
	return true
}

func DeleteVulnerability(id int) bool {
	global.GVA_DB.Model(&Vulnerability{}).Where("id = ?", id).Delete(&Vulnerability{})
	return true
}

func ExistVulnerabilityByID(id int) bool {
	var vul Vulnerability
	global.GVA_DB.Model(&Vulnerability{}).Where("id = ?", id).First(&vul)
	if vul.Id > 0 {
		return true
	}
	return false
}

func ExistVulnerabilityByNameZh(name_zh string) bool {
	var vul Vulnerability
	global.GVA_DB.Model(&Vulnerability{}).Where("name_zh = ?", name_zh).First(&vul)
	if vul.Id > 0 {
		return true
	}
	return false
}
